#!/usr/bin/python
# -*- coding: utf-8 -*-

'''Quick-and-dirty implementations of Weinbull forgetting function in a variety of retrieval
and analysis tasks'''

backup = {'2013-02-04':'wiki_2013_02_04', '2013-04-19':'wiki_2013_04_19', '2013-05-22':'wiki_2013_05_22', '2013-06-28':'wiki_2013_06_28', '2013-07-24':'wiki_2013_07_24', '2013-08-31':'wiki_2013_08_31'}

prefix = '/home/collections/L3S_wiki_backups'
access_suffix = 'data/access.log'
pages_prefix = 'data/pages'

import os
from datetime import datetime
import math

# Return access log
def access(d):
    f = os.path.join(prefix,backup[d],access_suffix)
    return f

# Return list of pages
def pages(ns,d):
    res = []
    f = os.path.join(prefix,backup[d],pages_prefix,ns)
    for root,di,files in os.walk(f):
        [res.append(fi) for fi in files]
    return res

def days_between(d1, d2):
    return abs((d2 - d1).days)


cache_log_file = dict()

# Calculate the memory buoyancy of a resource (namespace, page)
def mbp(ps,ns,dn):
    (a,d,b,u) = (0.009,0.7,0.1,0.7)
    currenttime = datetime.strptime(dn,"%Y-%m-%d")
    logfile = access(dn)
    lasttime = None
    if logfile not in cache_log_file:
        lst = []
        with open(logfile,'r') as f:
            [lst.append(line) for line in f]
        cache_log_file[logfile] = lst
    else:
        lst = cache_log_file[logfile]

    for line in lst:
        i = line.find('[')
        j = line.find(' ',i)
        dt = datetime.strptime(line[i+1:j],'%d/%b/%Y:%H:%M:%S')
        # dstr = dt.strftime('%Y-%m-%d')
        i = line.find('"GET')
        j = line.find(ns,i+1)
        if j > 0:
            k = line.find(ps,len(ns)+i+1)
            if k > 0:
                lasttime = dt

    delta = days_between(currenttime,lasttime)
    # print delta
    wb = b + (1-b)*u*math.exp(0-math.pow(a*delta, d)/float(d))
    return wb
        
# Calculate the memory buoyancy of a namespace in L3S wiki backups
# by estimating the forgetability of its pages w.r.t current date
def mb(ns,d):
     ps = pages(ns,d)
     wb = 0
     s = 0
     if not ps: return 0
     for p in ps:
         pb = os.path.basename(p)
         i = pb.rfind('.')
         pn = pb[:i]
         wb += mbp(pn,ns,d)
         s += 1
     mbv = wb / float(s)
     return mbv

ns = ['duraark','cubrik','terence','vifachem','liwa','m-eco','linkedup']
dlst = ['2013-02-04','2013-04-19','2013-05-22']
import itertools
def test_mb():
    for (n,ds) in itertools.product(ns,dlst):
        print '%s\t%s: %.5f' % (n,ds,mb(n,ds))
    
import sys
# f = pages(sys.argv[1],sys.argv[2])
# print f
# pages(sys.argv[1],sys.argv[2])
# print mb(sys.argv[1],sys.argv[2])
test_mb()